package com.zhiyun.yycall.ui;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.zhiyun.common.YYCallUtils;
import com.zhiyun.message.ForgetPwdRequest;
import com.zhiyun.message.ForgetPwdResponse;
import com.zhiyun.message.LoginRequest;
import com.zhiyun.message.LoginResponse;
import com.zhiyun.net.AsynCallback;
import com.zhiyun.net.Net;
import com.zhiyun.net.Response;
import com.zhiyun.trace.TraceActivity;
import com.zhiyun.yycall.Config;
import com.zhiyun.yycall.R;

public class LoginActivity extends TraceActivity {

	private EditText phoneNoET;
	private EditText passwordET;

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.regBtn:
				gotoReg();
				break;
			case R.id.loginBtn:

				final String phoneNo = phoneNoET.getText().toString();
				final String password = passwordET.getText().toString();
				if (StringUtils.isBlank(phoneNo)) {
					AndroidUtils.toastShort("请输入手机号码");
					return;

				}

				if (StringUtils.isBlank(password)) {
					AndroidUtils.toastShort("请输入登录密码");
					return;

				}

				remoteLogin(phoneNo, password);
				break;

			case R.id.forgetPwdBtn:
				final String pwd_phoneNo = phoneNoET.getText().toString();
				if (StringUtils.isBlank(pwd_phoneNo)) {
					AndroidUtils.toastShort("请输入手机号码");
					return;
				}

				AndroidUtils.Dialog.confirm(LoginActivity.this, "提示",
						String.format("确认要重置【%s】密码？", pwd_phoneNo),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								remoteForgetPwd(pwd_phoneNo);

							}

						}

				);

				break;

			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.login_activity);
		phoneNoET = (EditText) this.findViewById(R.id.phoneNo);
		passwordET = (EditText) this.findViewById(R.id.password);

		phoneNoET
				.setText(StringUtils.isBlank(Config.getPhoneNo()) ? AndroidUtils.System
						.getPhoneNo() : Config.getPhoneNo());
		passwordET.setText(Config.getPlainPassword());

		this.findViewById(R.id.regBtn).setOnClickListener(listener);
		this.findViewById(R.id.loginBtn).setOnClickListener(listener);

		TextView forgetPwdView = (TextView) this
				.findViewById(R.id.forgetPwdBtn);
		forgetPwdView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);// 底部加横线
		forgetPwdView.setOnClickListener(listener);

	}

	public void remoteForgetPwd(String phoneNo) {

		ForgetPwdRequest req = new ForgetPwdRequest();
		req.setPhoneNo(phoneNo);

		Net.doHttpRemote(this, req, new ForgetPwdResponse(),
				new AsynCallback<Response>() {

					@Override
					public boolean callback(Response r) {
						if (r.isFail()) {

							switch (r.getErrorCode()) {
							case Response.ACTIVATE_CODE_REQUEST_FREQUENCY:
								AndroidUtils
										.toastShort("密码重置请求已经发出，请耐性等待短信验证码，1分钟后重新请求");
								break;
							default:
								AndroidUtils.toastShort("重设密码的请求发送失败，原因："
										+ r.getError());
								break;
							}

							return true;
						}

						// 进入等待注册码的界面
						gotoResetPwdActivity();
						return true;

					}
				});

	}

	private void remoteLogin(final String phoneNo, final String password) {

		LoginRequest req = new LoginRequest();
		req.setPhoneNo(phoneNo);

		req.setPassword(YYCallUtils.encryptPwd(phoneNo, password));

		Net.doHttpRemote(this, req, new LoginResponse(),
				new AsynCallback<Response>() {

					@Override
					public boolean callback(Response r) {
						if (r.isFail()) {
							switch (r.getErrorCode()) {
							case Response.WAITTING_ACTIVE:// 用户处于待激活状态，直接进入激活界面
								gotoActiveActivity();
								break;
							case Response.ACCESS_DENY:
								AndroidUtils.toastShort("登录没成功：账号或者密码错误");
								break;
							case Response.USER_NOT_REGIESTED:
								AndroidUtils.toastLong("该号码暂未注册,请先注册");
								gotoReg();// 用户未注册
								break;

							default:
								AndroidUtils.toastShort("登录没成功：" + r.getError());

							}

							return true;
						}

						Config.setPhoneNo(phoneNo);
						Config.setPassword(password);

						gotoMainActivity();
						return true;

					}
				});

	}

	private final static int REG_CODE = 1;
	private final static int ACTIVATE_CODE = 2;
	private final static int RESET_PWD = 3;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REG_CODE:
		case ACTIVATE_CODE:
			if (resultCode == Activity.RESULT_OK) {
				this.finish();
				this.gotoMainActivity();
				return;
			}
			break;

		case RESET_PWD:

			// 重置密码成功直接做登录操作
			if (resultCode == Activity.RESULT_OK) {
				String password = data.getStringExtra("password");
				passwordET.setText(password);
				remoteLogin(phoneNoET.getText().toString(), password);

				return;
			}

			break;

		}

	}

	private void gotoActiveActivity() {
		Intent intent = new Intent();
		intent.setClass(this, ActivateActivity.class);
		intent.putExtra("phoneNo", phoneNoET.getText().toString());
		this.startActivityForResult(intent, ACTIVATE_CODE);
	}

	private void gotoResetPwdActivity() {

		Intent intent = new Intent();
		intent.setClass(this, ResetPwdActivity.class);
		intent.putExtra("phoneNo", phoneNoET.getText().toString());
		this.startActivityForResult(intent, RESET_PWD);

	}

	private void gotoReg() {
		Intent intent = new Intent();
		intent.setClass(this, RegistActivity.class);
		this.startActivityForResult(intent, REG_CODE);
	}

	private void gotoMainActivity() {

		Intent intent = new Intent();
		intent.setClass(this, MainActivity.class);
		this.startActivity(intent);
		LoginActivity.this.finish();

	}

	@Override
	protected void onResume() {
		super.onResume();

		String phoneNo = phoneNoET.getText().toString();
		if (!StringUtils.isBlank(phoneNo)) {
			phoneNoET.setSelection(phoneNo.length());
		}

	}

}
